package parasoft.centrap.test;

public class Browser {

	private String ua = "";
	private boolean isOpera = false;
	private boolean isChrome = false;
	private boolean isWebKit = false;
	private boolean isSafari = false;
	private boolean isSafari2 = false;
	private boolean isSafari3 = false;
	private boolean isSafari4 = false;
	private boolean isIE = false;
	private boolean isIE7 = false;
	private boolean isIE8 = false;
	private boolean isIE6 = false;
	private boolean isGecko = false;
	private boolean isGecko2 = false;
	private boolean isGecko3 = false;
	private boolean isWindows = false;
	private boolean isMac = false;
	private boolean isAir = false;
	private boolean isLinux = false;
	
	public boolean isOpera() {
		return isOpera;
	}

	public boolean isChrome() {
		return isChrome;
	}

	public boolean isWebKit() {
		return isWebKit;
	}

	public boolean isSafari() {
		return isSafari;
	}

	public boolean isSafari2() {
		return isSafari2;
	}

	public boolean isSafari3() {
		return isSafari3;
	}

	public boolean isSafari4() {
		return isSafari4;
	}

	public boolean isIE() {
		return isIE;
	}

	public boolean isIE7() {
		return isIE7;
	}

	public boolean isIE8() {
		return isIE8;
	}

	public boolean isIE6() {
		return isIE6;
	}

	public boolean isGecko() {
		return isGecko;
	}

	public boolean isGecko2() {
		return isGecko2;
	}

	public boolean isGecko3() {
		return isGecko3;
	}

	public boolean isWindows() {
		return isWindows;
	}

	public boolean isMac() {
		return isMac;
	}

	public boolean isAir() {
		return isAir;
	}

	public boolean isLinux() {
		return isLinux;
	}

	private Browser(final String ua){
		this.ua = ua.toLowerCase();
        this.isOpera = check("opera");
        this.isChrome = check("\bchrome\b");
        this.isWebKit = check("webkit");
        this.isSafari = !isChrome && check("safari");
        this.isSafari2 = isSafari && check("applewebkit\\/4"); // unique to Safari 2
        this.isSafari3 = isSafari && check("version\\/3");
        this.isSafari4 = isSafari && check("version\\/4");
        this.isIE = !isOpera && check("msie");
        this.isIE7 = isIE && check("msie 7");
        this.isIE8 = isIE && check("msie 8");
        this.isIE6 = isIE && !isIE7 && !isIE8;
        this.isGecko = !isWebKit && check("gecko");
        this.isGecko2 = isGecko && check("rv:1\\.8");
        this.isGecko3 = isGecko && check("rv:1\\.9");
        this.isWindows = check("windows|win32");
        this.isMac = check("macintosh|mac os x");
        this.isAir = check("adobeair");
        this.isLinux = check("linux");
	}
	
	public static Browser forUserAgent(final String ua){
		return new Browser(ua);
	}
	
	private boolean check(final String regex){
		return ua.matches(".*"+regex+".*");
	}
	
	public static void main(final String[] args) {

	}

	
	@Override
	public String toString() {
		return ua;
	}
}
